﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace PMP.Core.Extensions;

public static class CaptchaHelper
{
    private static readonly Random _random = new Random();
    private static readonly string[] _operators = { "+", "-", "*", "/" };
    private static readonly int _maxNumber = 10;

    public static CaptchaResult GenerateCaptcha()
    {
        // 生成随机运算
        string op = _operators[_random.Next(0, 4)];
        int num1, num2, answer;
        string question;

        do
        {
            num1 = _random.Next(1, _maxNumber);
            num2 = _random.Next(1, _maxNumber);

            switch (op)
            {
                case "+":
                    answer = num1 + num2;
                    question = $"{num1} + {num2} = ?";
                    break;
                case "-":
                    // 确保减法结果非负
                    if (num1 < num2) (num1, num2) = (num2, num1);
                    answer = num1 - num2;
                    question = $"{num1} - {num2} = ?";
                    break;
                case "*":
                    answer = num1 * num2;
                    question = $"{num1} × {num2} = ?";
                    break;
                case "/":
                    // 确保除法能整除
                    int product = num1 * num2;
                    answer = num2;
                    question = $"{product} ÷ {num1} = ?";
                    break;
                default:
                    throw new ArgumentException("Invalid operator");
            }
        } while (answer < 0 || answer > 20); // 控制答案范围

        // 生成图片
        byte[] imageBytes = CreateCaptchaImage(question);
        string base64Image = Convert.ToBase64String(imageBytes);

        return new CaptchaResult
        {
            Base64Image = "data:image/png;base64,"+ base64Image,
            Answer = answer.ToString()
        };
    }

    private static byte[] CreateCaptchaImage(string text)
    {
        int width = 120;
        int height = 40;

        using (Bitmap bitmap = new Bitmap(width, height))
        using (Graphics g = Graphics.FromImage(bitmap))
        using (MemoryStream ms = new MemoryStream())
        {
            // 设置背景
            g.Clear(Color.White);

            // 绘制干扰线
            for (int i = 0; i < 5; i++)
            {
                using (Pen pen = new Pen(Color.FromArgb(
                    _random.Next(50, 200),
                    _random.Next(50, 200),
                    _random.Next(50, 200))))
                {
                    g.DrawLine(pen,
                        _random.Next(width), _random.Next(height),
                        _random.Next(width), _random.Next(height));
                }
            }

            // 添加噪点
            for (int i = 0; i < 100; i++)
            {
                bitmap.SetPixel(
                    _random.Next(width),
                    _random.Next(height),
                    Color.FromArgb(_random.Next(255), _random.Next(255), _random.Next(255)));
            }

            // 设置字体
            Font font = new Font("Arial", 16, FontStyle.Bold);
            
            // 绘制文本
            g.DrawString(text, font, Brushes.Black, 15, 10);

            // 添加边框
            g.DrawRectangle(Pens.LightGray, 0, 0, width - 1, height - 1);

            // 保存为PNG
            bitmap.Save(ms, ImageFormat.Png);
            return ms.ToArray();
        }
    }
}

public class CaptchaResult
{
    public string Base64Image { get; set; }
    public string Answer { get; set; }
}
